package org.budo.graph.entity;

import java.io.Serializable;

import org.budo.ehcache.config.annotation.entity.EhCacheConfigEntity;
import org.budo.graph.service.util.EhcacheStatisticsUtil;

import lombok.Getter;
import lombok.Setter;
import net.sf.ehcache.Cache;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.statistics.StatisticsGateway;

/**
 * @author li
 */
@Getter
@Setter
public class EhcacheStatistics implements Serializable {
    private static final long serialVersionUID = 1873944701630217352L;

    private Cache cache;

    public EhcacheStatistics(Cache cache) {
        this.setCache(cache);
    }

    public Boolean getConfigGraph() {
        EhCacheConfigEntity configEntity = this.getConfigEntity();
        return null != configEntity && configEntity.getGraph();
    }

    public EhCacheConfigEntity getConfigEntity() {
        String cacheName = this.getCacheName();
        return EhcacheStatisticsUtil.getEhCacheConfigEntity(cacheName);
    }

    public StatisticsGateway getStatistics() {
        return this.getCache().getStatistics();
    }

    public CacheConfiguration getConfiguration() {
        return this.getCache().getCacheConfiguration();
    }

    public String getCacheName() {
        return this.getCache().getName();
    }

    public Integer getKeyCount() {
        return this.getCache().getKeys().size();
    }

    public Long getCacheHitCount() {
        return this.getStatistics().cacheHitCount();
    }

    public Long getLocalHeapHitCount() {
        return this.getStatistics().localHeapHitCount();
    }

    public Long getMaxEntriesLocalDisk() {
        return this.getConfiguration().getMaxEntriesLocalDisk();
    }

    public Long getLocalDiskHitCount() {
        return this.getStatistics().localDiskHitCount();
    }

    public Long getCacheMissCount() {
        return this.getStatistics().cacheMissCount();
    }

    public Long getLocalHeapMissCount() {
        return this.getStatistics().localHeapMissCount();
    }

    public Long getCacheMissExpiredCount() {
        return this.getStatistics().cacheMissExpiredCount();
    }

    public Long getCacheMissNotFoundCount() {
        return this.getStatistics().cacheMissNotFoundCount();
    }

    public Long getCachePutCount() {
        return this.getStatistics().cachePutCount();
    }

    public Long getLocalHeapPutCount() {
        return this.getStatistics().localHeapPutCount();
    }

    public Long getCachePutAddedCount() {
        return this.getStatistics().cachePutAddedCount();
    }

    public Long getLocalHeapPutAddedCount() {
        return this.getStatistics().localHeapPutAddedCount();
    }

    public Long getCacheRemoveCount() {
        return this.getStatistics().cacheRemoveCount();
    }

    public Long getCacheEvictedCount() {
        return this.getStatistics().cacheEvictedCount();
    }

    public Long getCacheExpiredCount() {
        return this.getStatistics().cacheExpiredCount();
    }

    public Long getTimeToLiveSeconds() {
        return this.getConfiguration().getTimeToLiveSeconds();
    }

    public Long getTimeToIdleSeconds() {
        return this.getConfiguration().getTimeToIdleSeconds();
    }

    public Long getMaxEntriesLocalHeap() {
        return this.getConfiguration().getMaxEntriesLocalHeap();
    }

    public Double getMissExpiredRatio() {
        long cacheHitCount = this.getStatistics().cacheHitCount();
        double denominator = cacheHitCount != 0 ? cacheHitCount : 1;

        long cacheMissExpiredCount = this.getStatistics().cacheMissExpiredCount();
        return (100 * cacheMissExpiredCount) / denominator;
    }

    public Double getMissNotFoundRatio() {
        long cacheHitCount = this.getStatistics().cacheHitCount();
        double denominator = cacheHitCount != 0 ? cacheHitCount : 1;

        long cacheMissNotFoundCount = this.getStatistics().cacheMissNotFoundCount();
        return (100 * cacheMissNotFoundCount) / denominator;
    }
}